﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

namespace TotalSport
{
    public partial class Invoice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["TotalSport"].ConnectionString;
            if (Session["UserId"] == null || Session["UserId"].ToString() == "")
            {
                Session.Abandon();
                Session.RemoveAll();
                Response.Redirect("LoginUser.aspx");
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["refno"] != null)
                {
                    int referenceno = Convert.ToInt32(Request.QueryString["refno"]);
                    ViewState["Refno"] = referenceno;
                    lblRefNo.Text = referenceno.ToString();
                    string[] detaills = BAL.Customers.getpernldetail(referenceno);

                    lblid.Text = detaills[0];
                    lblname.Text = detaills[2] + detaills[3];
                    lblCompanyname.Text = detaills[6];
                    lblcontactno.Text = detaills[5];
                    lblemailid.Text = detaills[4];
                    lbllocation.Text = detaills[11];
                    getproduct();
                    if (grdinvoicedtl.Rows.Count > 0)
                    {
                        foreach (GridViewRow gvRow in grdinvoicedtl.Rows)
                        {//find your textbox
                            TextBox txtqty = (TextBox)gvRow.FindControl("txtquanty");
                            txtqty.Text = "1";
                        }
                    }
                }
            }
        }
        protected void grdinvoicedtl_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdinvoicedtl.PageIndex = e.NewPageIndex;
            getproduct();
        }
        public void getproduct()
        {
            int refereno = Convert.ToInt32(ViewState["Refno"]);
            DataSet ds = BAL.Customers.getproductquote(refereno);
            grdinvoicedtl.DataSource = ds;
            grdinvoicedtl.DataBind();
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {

            int refereno = Convert.ToInt32(ViewState["Refno"]);
            BAL.Customers.UpdateInvoiceColumn(refereno);
            int personalid = Convert.ToInt32(lblid.Text);
            if (grdinvoicedtl.Rows.Count > 0)
            {
                foreach (GridViewRow gvRow in grdinvoicedtl.Rows)
                {//find your textbox
                    TextBox txtqty = (TextBox)gvRow.FindControl("txtquanty");
                    int qty = Convert.ToInt32(txtqty.Text);
                    int subid = Convert.ToInt32(grdinvoicedtl.DataKeys[gvRow.RowIndex]["SubId"]);
                    Label lbltotal = (Label)gvRow.FindControl("lblcalcqtyprice");
                    int toalprice = Convert.ToInt32(lbltotal.Text);
                    BAL.Customers.insertinvoicedetl(personalid, subid, qty, toalprice, refereno);
                }
            }
            spnMsg.Style.Add("Display", "block");
            spnMsg.InnerHtml = "Invoice Created Successfully";
            if (grdinvoicedtl.Rows.Count > 0)
            {
                foreach (GridViewRow gvRow in grdinvoicedtl.Rows)
                {//find your textbox
                    TextBox txtqty = (TextBox)gvRow.FindControl("txtquanty");
                    txtqty.Enabled = false;
                    btnsave.Enabled = false;
                    btncancel.Enabled = false;
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int referenceno = Convert.ToInt32(Request.QueryString["refno"]);
            Response.Redirect("ShowQuotes.aspx?refno=" + referenceno);
        }

        protected void txtquanty_TextChanged(object sender, EventArgs e)
        {
            if (grdinvoicedtl.Rows.Count > 0)
            {
                foreach (GridViewRow gvRow in grdinvoicedtl.Rows)
                {//find your textbox
                    TextBox txtqty = (TextBox)gvRow.FindControl("txtquanty");

                    Label oldprice = (Label)gvRow.FindControl("lblprice");

                    int price = Convert.ToInt32(oldprice.Text);

                    int qty = Convert.ToInt32(txtqty.Text);

                    int total = qty * price;
                    Label lblprice = (Label)gvRow.FindControl("lblcalcqtyprice");
                    lblprice.Text = total.ToString();





                }
            }

        }



    }
}